import {RequestRs} from '../../modules/Request-rs';
import {ReponseRs} from '../../modules/Response-rs';
import {Product} from '../../modules/product';
import {SCItem} from '../../modules/scitem';
import {User} from '../../modules/user';
import {HttpAssist} from '../../share/assist/http-assist';
import {HttpService} from '../../share/service/http.service';
import {UserService} from '../../share/service/user.service';
import {Component, OnInit} from '@angular/core';

@Component({
  selector: 'app-shopping-cart',
  templateUrl: './shopping-cart.component.html',
  styleUrls: ['./shopping-cart.component.css']
})
export class ShoppingCartComponent implements OnInit {
  private goodList: SCItem[];
  req: RequestRs = new RequestRs();
  res: ReponseRs = new ReponseRs();
  private user: User;

  constructor(
    private userService: UserService, private httpService: HttpService
  ) {

  }

  public initShoppinCart() {
    const serviceName = 'initShoppinCart';
    this.req.path = '/' + this.user.id;
    this.req.param = '';
    this.req.body = '';
    const message = JSON.stringify(this.req);
    this.httpService.postByJson(serviceName, HttpAssist.GET_PATH, message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS) {
          const gl: SCItem[] = JSON.parse(this.res.message);
          this.setGoodList = gl;
        } else {
          alert('fail, detail{' + 'code:' + this.res.status + ';message:' + this.res.message + '}');
        }
      },
      error => {
        alert('fail,http error in UI, detail' + error);
      }
    );
  }

  public addProductToSC(product: Product) {
    const serviceName = 'addProductToSC';
    this.req.path = '/' + this.user.id + '/' + product.id;
    this.req.param = '';
    this.req.body = '';
    const message = JSON.stringify(this.req);
    this.httpService.postByJson(serviceName, HttpAssist.GET_PATH, message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS) {
          const proInSC = this.getGoodList.find(item => item.product.id === product.id);
          proInSC.total++;
          alert('The product have been added to the shopping cart!');
        } else {
          alert('fail, detail{' + 'code:' + this.res.status + 'message:' + this.res.message + '}');
        }
      },
      error => {
        alert('fail,http error in UI, detail' + error);
      }
    );
  }
  public removeOneProductfromSC(product: Product) {
    const serviceName = 'removeOneProductfromSC';
    this.req.path = '/' + this.user.id + '/' + product.id;
    this.req.param = '';
    this.req.body = '';
    const message = JSON.stringify(this.req);
    this.httpService.postByJson(serviceName, HttpAssist.GET_PATH, message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS && this.res.message === 'true') {
          const proInSC = this.getGoodList.find(item => item.product.id === product.id);
          if (proInSC.total !== 0) {
            proInSC.total--;
            if (proInSC.total === 0) {
              if (this.getGoodList.length === 1) {
                this.setGoodList = null;
              } else {
                let index = this.getGoodList.indexOf(proInSC)
                this.getGoodList.splice(index, 1);
              }
            }
          }
          alert('The product have been removed to the shopping cart!');
        } else {
          alert('fail, detail{' + 'code:' + this.res.status + '; message:' + this.res.message + '}');
        }
      },
      error => {
        alert('fail,http error in UI, detail' + error);
      }
    );
  }

  public removeAllProductfromSC(product: Product) {
    const serviceName = 'removeAllProductfromSC';
    this.req.path = '/' + this.user.id + '/' + product.id;
    this.req.param = '';
    this.req.body = '';
    const message = JSON.stringify(this.req);
    this.httpService.postByJson(serviceName, HttpAssist.GET_PATH, message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS && this.res.message === 'true') {
          if (this.getGoodList.length === 1) {
            this.setGoodList = null;
          } else {
            const proInSC = this.getGoodList.find(item => item.product.id === product.id);
            const index = this.getGoodList.indexOf(proInSC)
            this.getGoodList.splice(index, 1);
          }
          alert('All product have been removed to the shopping cart!');
        } else {
          alert('fail, detail{' + 'code:' + this.res.status + '; message:' + this.res.message + '}');
        }
      },
      error => {
        alert('fail,http error in UI, detail' + error);
      }
    );
  }

  get getGoodList(): SCItem[] {
    return this.goodList;
  }

  set setGoodList(gl: SCItem[]) {
    this.goodList = gl;
  }
  ngOnInit() {
    this.user = this.userService.getUser;
    this.initShoppinCart();
  }
}
